<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="700" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
			<el-form-item label="品牌名称" prop="name">
				<el-input v-model="form.name" :maxlength="255" show-word-limit></el-input>
			</el-form-item>
			<el-form-item label="品牌LOGO" prop="logo">
				<ImageUpload 
					v-model="form.logo" 
					:width="120" 
					:height="120"
					placeholder="上传LOGO"
					tip="支持 JPG、PNG、GIF 格式，大小不超过10MB"
					@success="handleLogoUploadSuccess"
					@error="handleLogoUploadError"
					@delete="handleLogoDelete" />
			</el-form-item>
			<el-form-item label="品牌分类" prop="category_ids">
				<el-select 
					v-model="form.category_ids" 
					multiple 
					placeholder="请选择品牌分类"
					style="width: 100%;"
					:loading="categoryLoading">
					<el-option
						v-for="item in categoryOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="品牌描述" prop="description">
				<el-input 
					v-model="form.description" 
					type="textarea" 
					:rows="4"
					:maxlength="500" 
					show-word-limit
					placeholder="请输入品牌描述">
				</el-input>
			</el-form-item>
			<el-form-item label="排序值" prop="sort">
				<el-input-number v-model="form.sort" :min="0" :max="255" controls-position="right" style="width: 200px;"></el-input-number>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-radio v-model="form.status" :label="1">启用</el-radio>
				<el-radio v-model="form.status" :label="2">禁用</el-radio>
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="visible = false">取 消</el-button>
				<el-button type="primary" @click="optSubmit('ruleForm')" :loading="loading">确 定</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script>
import ImageUpload from '@/components/ImageUpload.vue'

export default {
	components: {
		ImageUpload
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增品牌',
				edit: '编辑品牌',
				show: '查看'
			},
			form: {
				id: 0,
				name: '',
				logo: '',
				description: '',
				category_ids: [],
				sort: 0,
				status: 1
			},
			rules: {
				name: [
					{required: true, message: '请输入品牌名称', trigger: 'blur'}
				],
				sort: [
					{required: true, message: '请输入排序值', trigger: 'blur'}
				],
				status: [
					{required: true, message: '请选择状态', trigger: 'change'}
				]
			},
			categoryOptions: [],
			categoryLoading: false,
			loading: false,
			visible: false
		}
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			this.getCategoryList();
			return this
		},
		// 获取品牌分类列表
		async getCategoryList() {
			this.categoryLoading = true;
			try {
				// 调用品牌分类接口，设置大一点的分页大小
				let res = await this.$API.brandCate.list.get({
					limit: 1000,
					page: 1,
					name: ''
				});
				if (res.code == 0) {
					this.categoryOptions = res.data.data;
				}
			} catch (error) {
				console.error('获取品牌分类失败:', error);
			}
			this.categoryLoading = false;
		},
		// 表单提交方法
		optSubmit(formName) {
			this.$refs[formName].validate(async (valid) => {
				if (valid) {
					this.loading = true
					let res;
					if (this.mode == 'add') {
						res = await this.$API.brand.add.post(this.form)
					} else {
						res = await this.$API.brand.edit.post(this.form)
					}
					this.loading = false

					if (res.code == 0) {
						this.$message.success(res.msg)
						this.$emit('success')
					} else {
						this.$message.error(res.msg)
					}
				} else {
					return false;
				}
			})
		},
		// 表单注入数据
		setData(item) {
			this.form.id = item.id
			this.form.name = item.name
			this.form.logo = item.logo || ''
			this.form.description = item.description || ''
			this.form.sort = item.sort
			this.form.status = item.status
			this.form.category_ids = item.category_ids || []
		},
		// LOGO上传成功回调
		handleLogoUploadSuccess(data) {
			console.log('LOGO上传成功:', data);
		},
		// LOGO上传失败回调
		handleLogoUploadError(error) {
			console.error('LOGO上传失败:', error);
		},
		// LOGO删除回调
		handleLogoDelete() {
			console.log('LOGO已删除');
			// 这里可以添加额外的删除逻辑，比如删除服务器上的文件
		}
	}
}
</script>

<style scoped>
</style>